package cn.edu.swu.shopcar;

import cn.edu.swu.book.BOOK;

import java.util.*;

public class shoppingCar {
    private Map<Integer,CarItem> map=new HashMap<>();

    public void add(BOOK book){
        if(map.containsKey(book.getId())){
            CarItem item=map.get(book.getId());
            item.setNum(item.getNum()+1);
            map.put(book.getId(),item);
        }else{
            CarItem item=new CarItem();
            item.setBook(book);
            item.setNum(1);
            map.put(book.getId(),item);
        }
    }

    public void delete(Integer id){
        map.remove(id);
    }

    public void increase(Integer id){
        CarItem item=map.get(id);
        System.out.println(item.getNum());
        item.setNum(item.getNum()+1);
        map.put(id,item);
    }

    public void decrease(Integer id){
        CarItem item=map.get(id);
        if(item.getNum()==1) this.delete(id);
        else{
            item.setNum(item.getNum()-1);
            map.put(id,item);}
    }


    public void changePrice(Integer id,Integer num){
        CarItem item=map.get(id);
        item.setNum(num);
    }

    public Map<Integer,CarItem> getMap(){
        return map;
    }
}
